//! 错误处理模块
//! 
//! 定义汇编器可能产生的各种错误类型

use std::fmt;

/// 汇编器结果类型
pub type Result<T> = std::result::Result<T, AssemblerError>;

/// 汇编器错误类型
#[derive(Debug, Clone)]
pub enum AssemblerError {
    /// 词法分析错误（无法识别的字符或符号）
    LexerError {
        message: String,
        line: usize,
        column: usize,
    },
    /// 语法分析错误（语法不正确）
    ParseError {
        message: String,
        line: usize,
        column: usize,
    },
    /// 指令未找到错误
    InstructionNotFound {
        instruction: String,
        line: usize,
    },
    /// 操作数错误（数量或类型不正确）
    OperandError {
        message: String,
        line: usize,
    },
    /// 代码生成错误
    CodeGenError {
        message: String,
    },
    /// IO 错误
    IoError {
        message: String,
    },
}

impl fmt::Display for AssemblerError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AssemblerError::LexerError { message, line, column } => {
                write!(f, "词法错误 [{}:{}]: {}", line, column, message)
            }
            AssemblerError::ParseError { message, line, column } => {
                write!(f, "语法错误 [{}:{}]: {}", line, column, message)
            }
            AssemblerError::InstructionNotFound { instruction, line } => {
                write!(f, "未找到指令 [{}]: {}", line, instruction)
            }
            AssemblerError::OperandError { message, line } => {
                write!(f, "操作数错误 [{}]: {}", line, message)
            }
            AssemblerError::CodeGenError { message } => {
                write!(f, "代码生成错误: {}", message)
            }
            AssemblerError::IoError { message } => {
                write!(f, "IO 错误: {}", message)
            }
        }
    }
}

impl std::error::Error for AssemblerError {}

